const {
  getBlogList,
  getBlogDetail,
  createBlog,
  updateBlog,
  deleteBlog
} = require('../controller/blog')
const { SuccessModel, ErrorModel } = require('../model/resModel')

const blogHandleRouter = (req, res) => {
  // 博客列表
  if (req.method === 'GET' && req.path === '/api/blog/list') {
    return getBlogList(req.query).then(data => {
      if (data) {
        return new SuccessModel(data)
      } else {
        return new ErrorModel(data)
      }
    })
  }

  // 博客详情
  if (req.method === 'GET' && req.path === '/api/blog/detail') {
    return getBlogDetail(req.query).then(data => {
      if (data) {
        return new SuccessModel(data)
      } else {
        return new ErrorModel(data)
      }
    })
  }

  // 添加文章
  if (req.method === 'POST' && req.path === '/api/blog/add') {
    return createBlog(req.body).then(data => {
      if (data) {
        return new SuccessModel({}, '文章新增成功')
      } else {
        return new ErrorModel({}, '文章新增失败')
      }
    })
  }

  // 更新文章
  if (req.method === 'POST' && req.path === '/api/blog/update') {
    return updateBlog(req.body).then(data => {
      if (data) {
        return new SuccessModel({}, '文章更新成功')
      } else {
        return new ErrorModel({}, '文章更新失败')
      }
    })
  }

  // 删除文章
  if (req.method === 'POST' && req.path === '/api/blog/delete') {
    return deleteBlog(req.body).then(data => {
      if (data) {
        return new SuccessModel({}, '文章删除成功')
      } else {
        return new ErrorModel({}, '文章删除失败')
      }
    })
  }
}

module.exports = blogHandleRouter
